/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validaciones;

import javax.swing.JOptionPane;

/**
 *
 * @author David Vallejos A
 */
public class validacionRutBD {

    StringBuilder RutPropietarioMasc = new StringBuilder();

    public void valida(String rut) {

        String[] rutAlaBDfinal = new String[12];
        int[] respaldoRut = new int[9];

        int length = rut.length();
        int rutNro = Integer.parseInt(rut.replace(".", "").replace("-", "").replace("k", "0"));
        int verif = rutNro % 10;

        validacionesRegistros val = new validacionesRegistros();
        val.Calculos(rutNro);

        String verFinal = String.valueOf(val.getResta());

        for (int x = 8; x > -1; x--) {
            int resto = rutNro % 10;
            rutNro = rutNro / 10;
            respaldoRut[x] = resto;
        }

        int aux2 = val.getResta();

        if ((verif == 0) && (aux2 == 10)) {

            rutAlaBDfinal[0] = String.valueOf(respaldoRut[0]);
            rutAlaBDfinal[1] = String.valueOf(respaldoRut[1]);
            rutAlaBDfinal[2] = ".";
            rutAlaBDfinal[3] = String.valueOf(respaldoRut[2]);
            rutAlaBDfinal[4] = String.valueOf(respaldoRut[3]);
            rutAlaBDfinal[5] = String.valueOf(respaldoRut[4]);
            rutAlaBDfinal[6] = ".";
            rutAlaBDfinal[7] = String.valueOf(respaldoRut[5]);
            rutAlaBDfinal[8] = String.valueOf(respaldoRut[6]);
            rutAlaBDfinal[9] = String.valueOf(respaldoRut[7]);
            rutAlaBDfinal[10] = "-";
            rutAlaBDfinal[11] = "K";

        } else if ((verif == 0) && (aux2 == 11)) {

            rutAlaBDfinal[0] = String.valueOf(respaldoRut[0]);
            rutAlaBDfinal[1] = String.valueOf(respaldoRut[1]);
            rutAlaBDfinal[2] = ".";
            rutAlaBDfinal[3] = String.valueOf(respaldoRut[2]);
            rutAlaBDfinal[4] = String.valueOf(respaldoRut[3]);
            rutAlaBDfinal[5] = String.valueOf(respaldoRut[4]);
            rutAlaBDfinal[6] = ".";
            rutAlaBDfinal[7] = String.valueOf(respaldoRut[5]);
            rutAlaBDfinal[8] = String.valueOf(respaldoRut[6]);
            rutAlaBDfinal[9] = String.valueOf(respaldoRut[7]);
            rutAlaBDfinal[10] = "-";
            rutAlaBDfinal[11] = "0";

            for (int d = 0; d < rutAlaBDfinal.length; d++) {

                RutPropietarioMasc = RutPropietarioMasc.append(rutAlaBDfinal[d]);
            }
        }

        if (verif == val.getResta()) {

            rutAlaBDfinal[0] = String.valueOf(respaldoRut[0]);
            rutAlaBDfinal[1] = String.valueOf(respaldoRut[1]);
            rutAlaBDfinal[2] = ".";
            rutAlaBDfinal[3] = String.valueOf(respaldoRut[2]);
            rutAlaBDfinal[4] = String.valueOf(respaldoRut[3]);
            rutAlaBDfinal[5] = String.valueOf(respaldoRut[4]);
            rutAlaBDfinal[6] = ".";
            rutAlaBDfinal[7] = String.valueOf(respaldoRut[5]);
            rutAlaBDfinal[8] = String.valueOf(respaldoRut[6]);
            rutAlaBDfinal[9] = String.valueOf(respaldoRut[7]);
            rutAlaBDfinal[10] = "-";
            rutAlaBDfinal[11] = String.valueOf(respaldoRut[8]);

            for (int d = 0; d < rutAlaBDfinal.length; d++) {

                RutPropietarioMasc = RutPropietarioMasc.append(rutAlaBDfinal[d]);
            }
        }

        if (((verif != val.getResta())) && (verif != 0)) {//&& (val.getResta() != 10) && (val.getResta() != 11)) {

            respaldoRut[8] = val.getResta();

            rutAlaBDfinal[0] = String.valueOf(respaldoRut[0]);
            rutAlaBDfinal[1] = String.valueOf(respaldoRut[1]);
            rutAlaBDfinal[2] = ".";
            rutAlaBDfinal[3] = String.valueOf(respaldoRut[2]);
            rutAlaBDfinal[4] = String.valueOf(respaldoRut[3]);
            rutAlaBDfinal[5] = String.valueOf(respaldoRut[4]);
            rutAlaBDfinal[6] = ".";
            rutAlaBDfinal[7] = String.valueOf(respaldoRut[5]);
            rutAlaBDfinal[8] = String.valueOf(respaldoRut[6]);
            rutAlaBDfinal[9] = String.valueOf(respaldoRut[7]);
            rutAlaBDfinal[10] = "-";

            if (aux2 == 10) {
                rutAlaBDfinal[11] = "K";
            } else if (aux2 == 11) {
                rutAlaBDfinal[11] = "0";
            } else {
                rutAlaBDfinal[11] = String.valueOf(val.getResta());
            }

            for (int d = 0; d < rutAlaBDfinal.length; d++) {

                RutPropietarioMasc = RutPropietarioMasc.append(rutAlaBDfinal[d]);
            }

            String advRutInvílido = "";
            advRutInvílido += "El rut ingresado no es correcto\nel programa corrigió este error y ingresó el rut correcto\n\nRut: " + RutPropietarioMasc;
            //JOptionPane.showMessageDialog(null, advRutInvílido);

        }
    }

    public StringBuilder getRutPropietarioMasc() {
        return RutPropietarioMasc;
    }
}
